package com.yuelife.controller;

import com.yuelife.entity.CComment;
import com.yuelife.entity.CDiscussion;
import com.yuelife.service.CCommentService;
import com.yuelife.service.CDiscussionService;
import com.yuelife.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (CComment)表控制层
 *
 * @author makejava
 * @since 2020-06-12 15:55:16
 */
@Api(value = "评论controller", tags = "评论controller")
@RestController
@RequestMapping("cComment")
@CrossOrigin  //解决跨域
public class CCommentController {
    /**
     * 服务对象
     */
    @Resource
    private CCommentService cCommentService;

    @Autowired
    private CDiscussionService discussionService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据主键查询评论", notes = "主键查询评论，返回一个CComment对象")
    @ApiImplicitParam(paramType = "query", name = "id", value = "主键id", required = true, dataType = "Integer")
    @GetMapping("selectOne")
    public CComment selectOne(@ApiParam(name = "id", value = "评论主键id") Integer id) {
        return this.cCommentService.queryById(id);
    }

    /**
     * * 根据话题id查询到话题内容主体以及所有评论
     *
     * @Author: yangsen
     * create time: 2020/6/23 20:32
     */
    @ApiOperation(value = "根据帖子id查询评论", notes = "根据帖子id查询所有评论，返回ResultUtil")
    @ApiImplicitParam(paramType = "query", name = "id", value = "帖子id", required = true, dataType = "Integer")
    @ApiResponses({@ApiResponse(code = 200, message = "success"),
                    @ApiResponse(code = 400, message = "失败 ")})
    @GetMapping("/selectCommentByDiscussion")
    @ResponseBody
    public ResultUtil selectCommentByDiscussion(@ApiParam(name = "dId", value = "帖子主键id") Integer dId) {
        ResultUtil res = ResultUtil.success();
        //discussions当中有一个list，包含所有的评论
        CDiscussion discussions = cCommentService.selectCommentByDiscussion(dId);
        Map map = new HashMap();
        map.put("discussion", discussions);
        res.setResult(map);
        return res;
    }

    /**
     * 添加评论
     * 直接传入一个对象
     *
     * @Author: yangsen
     * create time: 2020/6/23 20:32
     */
    @Transactional
    @ApiOperation(value = "添加评论", notes = "添加评论，根据cComment内status属性判断是跟帖还是评论的回复")
    @ApiImplicitParam(paramType = "query", name = "cComment", value = "一个映射的CComment对象", required = true, dataType = "com.yuelife.entity.CComment")
    @ApiResponses({@ApiResponse(code = 200, message = "success"),
                    @ApiResponse(code = 400, message = "失败 ")})
    @PostMapping("/addComment")
    @ResponseBody
    public ResultUtil addComent(@RequestBody @ApiParam(name = "cComment", value = "一个完整的CComment对象") CComment cComment) {
        try {
            cCommentService.insert(cComment);
            if (cComment.getStatus() == 1) {
                CDiscussion discussion = new CDiscussion();
                //如果是话题下面的评论，更新最后回复时间以及回复数
                discussion.setLastReply(new Date());
                discussion.setId(cComment.getDiscusion());
                discussionService.updatereply(discussion);
            } else {
                //更新评论的回复数
                cCommentService.updatereply(cComment.getDiscusion());
            }
            return ResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.faild();
        }
    }

    /**
     * ·
     * 根据ID获取评论的详情，包括评论下面的评论
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取评论详情",notes = "根据id获取评论的详情，包括下面的评论")
    @ApiImplicitParam(paramType = "query",name = "id",value = "需要获取的commentid",required = true,dataType = "Integer")
    @ApiResponses({@ApiResponse(code = 200, message = "success"),
            @ApiResponse(code = 400, message = "失败 ")})
    @GetMapping("/comment")
    @ResponseBody
    public ResultUtil getComment(@ApiParam(name = "id",value = "评论的id") Integer id) {
        try {
            List<CComment> list = cCommentService.getComment(id);
            ResultUtil resultUtil = ResultUtil.success();
            Map map = new HashMap();
            map.put("comment", list.get(0));
            resultUtil.setResult(map);
            return resultUtil;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.faild();
        }
    }
    /**
     * 个人中心查询用户的所有评论
    * @Author: yangsen
    * create time: 2020/6/29 10:43
    */
    @ApiOperation(value = "获取个人所有评论",notes = "根据用户username获取用户发表的评论")
    @ApiImplicitParam(paramType = "query",name = "username",value = "需要获取的用户username",required = true,dataType = "String")
    @ApiResponses({@ApiResponse(code = 200, message = "success"),
            @ApiResponse(code = 400, message = "失败 ")})
    @GetMapping("/comment/{username}")
    @ResponseBody
    public ResultUtil getAllUsersComment(@PathVariable("username") String username){
        try {
            List<CComment> comments = cCommentService.selectAllUserComment(username);
            if(comments!=null){
                ResultUtil resultUtil = ResultUtil.success();
                Map map = new HashMap();
                map.put("comments",comments);
                resultUtil.setResult(map);
                return resultUtil;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.faild();
    }

}